module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  this.gameChanel = this.channelService.getChannel("GameChannel",true)
}

var handler = Handler.prototype

//开始匹配
handler.joinMatching = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var type = msg.type
	if(!type){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.joinMatching(null,uid,type,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//离开匹配
handler.leaveMatching = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.leaveMatching(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}


//获取金币场内玩家数量列表
handler.getGoldMatchPlyerCounts = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.getGoldMatchPlyerCounts(null,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}


//报名比赛场
handler.signUp = function(msg,session,next) {
	var uid = session.get("uid")
	var cid = msg.cid
	if(!uid || !cid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.signUp(null,uid,cid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}

//取消报名
handler.cancelSignUp = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.cancelSignUp(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取比赛场报名人数
handler.getCompetitionList = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.getCompetitionList(null,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取我的比赛场信息
handler.getCompetitionInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.game.remote.getCompetitionInfo(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取比赛场记录
handler.getRunCHistory = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.gameInfo.getRunCHistory(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}
//获取免费场参与次数
handler.getFreePlayCount = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
    this.app.rpc.game.remote.getFreePlayCount(null,uid,function(flag,msg) {
		next(null,{flag : flag,msg : msg})
	})
}